<template lang="pug">
    skin(@up="up",@down="down",@yes="yes",@back="back",@duringUp="up",@duringDown="down")
        div(class="flex-column large-dose-common h3")
            div(class="flex-column-item flex-center-center under-line")
                span --常规大剂量---
            div(class="flex-column-item flex-center")
                div(v-if="hasInfusion") {{meal}}剂量已输注！
                div(v-else, class="flex-row")
                    div(class="flex-row-item left flex-3 h3")
                        i(class="iconfont") &#xe631;
                        span {{meal}}剂量:
                    div(class="flex-row-item center active-block ")
                        span {{plan.amount}}U

            div(class="flex-column-item flex-center")
                div(class="flex-row" v-if="hasInfusion")
                    div(class="flex-row-item flex-2")
                        span  →追加剂量
                    div(class="flex-row-item")
                        span(class="active-block center",style="width:50%;display:inline-block") {{plan.amount}}U
                div(class="flex-row" v-else) 前一次输注:

            div(class="flex-column-item")
                div(class="flex-row" v-if="!lastInfusion")
                    div 暂无输注记录！
                div(class="flex-row",v-else)
                    div(class="flex-row-item") {{lastInfusion.date | format('MMDD')}}
                    div(class="flex-row-item center") {{lastInfusion.date | format('HH:mm')}}
                    div(class="flex-row-item right") {{lastInfusion.amount}}U
</template>

<script>
    import dayjs from "dayjs";
    import BaseVue from '@/mixins/BaseVue'

    export default {
        mixins: [BaseVue],
        data() {
            return {
                visible: false,
                hasInfusion: false,
                plan: {amount: null},
                meal: null
            };
        },
        methods: {
            getActive(id) {
                return this.menus.find(menu => {
                    return menu.id == id;
                });
            },
            up() {
                if (this.plan.amount < 20)
                    this.plan.amount = (this.plan.amount * 10 + 0.1 * 10) / 10;
            },
            down() {
                if (this.plan.amount > 0)
                    this.plan.amount = (this.plan.amount * 10 - 0.1 * 10) / 10;
            },

            back() {
                if (this.visible) this.visible = false;
                else this.$router.push("/large-dose/menu?index=1");
            },
            yes() {
                if (this.plan.amount > 0) {
                    // 添加纪录
                    window.dose.records.push({
                        amount: this.plan.amount,
                        date: new Date(),
                        type: null
                    });
                    this.$router.push("flusion");
                }
            }
        },
        computed : {
            lastInfusion(){
                let records = [].concat(window.dose.records).reverse();
                console.log(records[0]);
                return records[0]
            }
        },
        created() {
            const currentTimes = parseInt(dayjs().format("HHmm"));

            const isBreakfast =
                currentTimes < parseInt("0900") && currentTimes >= parseInt("0600");
            const isLunchTime =
                currentTimes < parseInt("1300") && currentTimes >= parseInt("1100");
            const isDinnerTime =
                currentTimes < parseInt("2000") && currentTimes >= parseInt("1700");

            this.plan.amount = window.dose.preset.breakfast;
            this.meal = "非三餐"; // 代表 时间段

            if (isBreakfast) {
                this.meal = "早餐";
                this.plan.amount = window.dose.preset.breakfast;
            }
            if (isLunchTime) {
                this.meal = "午餐";
                this.plan.amount = window.dose.preset.lunch;
            }
            if (isDinnerTime) {
                this.meal = "晚餐";
                this.plan.amount = window.dose.preset.dinner;
            }

            this.hasInfusion = false; // 当前常规剂量是否已输注
            const isSameDate = dayjs().format("MMDD") == dayjs(this.lastInfusion.date).format("MMDD");
            if (this.lastInfusion && isSameDate) {
                const lastTime = parseInt(dayjs(this.lastInfusion.date).format("HHmm")); // 最后输注时间
                switch (this.meal) {
                    case "早餐":
                        this.hasInfusion = lastTime < parseInt("1100") && lastTime >= parseInt("0000");
                        break;
                    case "午餐":
                        this.hasInfusion = lastTime < parseInt("1600") && lastTime >= parseInt("1100");
                        break;
                    case "晚餐":
                        this.hasInfusion = lastTime < parseInt("2400") && lastTime >= parseInt("1600");
                        break;
                }
            }
            if (this.hasInfusion) this.plan.amount = 0;
        }
    };
</script>

<style lang="less">
</style>
